package com.lagou.h2.consumer.controller;

import com.lagou.service.api.MethodService;
import com.lagou.util.ResponseStatisticsUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("test")
public class TestController {

    @Reference
    private MethodService methodService;

    final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(200, 500, 100L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100000));

    @GetMapping
    public void test() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("定时任务启动 start ------");
                int times = ThreadLocalRandom.current().nextInt(2000, 5000);
                System.out.println("执行次数:" + times);
                for (int i = 0; i < times; i++) {

                    //执行methodA
                    threadPoolExecutor.execute(() -> {
                        methodService.methodA();
                    });

                    //执行methodB
                    threadPoolExecutor.execute(() -> {
                        methodService.methodB();
                    });

                    //执行methodC
                    threadPoolExecutor.execute(() -> {
                        methodService.methodC();
                    });

                }
                System.out.println("定时任务启动 End ------");
            }
        }, 2000L, 60 * 1000L);


        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("---------------  TP START ---------------  ");
                System.out.println("methodA tp90:" + ResponseStatisticsUtil.getMethodResponseTp("methodA", 0.9));
                System.out.println("methodB tp90:" + ResponseStatisticsUtil.getMethodResponseTp("methodB", 0.9));
                System.out.println("methodC tp90:" + ResponseStatisticsUtil.getMethodResponseTp("methodC", 0.9));

                System.out.println("methodA tp99:" + ResponseStatisticsUtil.getMethodResponseTp("methodA", 0.99));
                System.out.println("methodB tp99:" + ResponseStatisticsUtil.getMethodResponseTp("methodB", 0.99));
                System.out.println("methodC tp99:" + ResponseStatisticsUtil.getMethodResponseTp("methodC", 0.99));

                System.out.println("---------------  TP END   ---------------  ");
            }
        }, 5000L, 5000L);

    }
}
